package nimy.info.android.dinnertime.data;

import info.nimy.google.recipesearch.html.RecipeItem;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nimy.info.android.dinnertime.model.RecipeKeyword;
import nimy.info.android.dinnertime.page.NoDuplicatedArrayList;

/**
 * 
 * @author 234aini@gmail.com 2012-10-29
 * 
 */
public class MemoryRecipeItemCache implements RecipeItemCache {

	private static final long serialVersionUID = 6354542134787079010L;
	private Map<RecipeKeyword, List<RecipeItem>> mData = new HashMap<RecipeKeyword, List<RecipeItem>>(
			10);

	@Override
	public List<RecipeItem> getCached(RecipeKeyword mKeyword) {
		List<RecipeItem> list = mData.get(mKeyword);
		if (list == null) {
			list = new NoDuplicatedArrayList<RecipeItem>(10);
			mData.put(mKeyword, list);
		}
		return list;
	}

	@Override
	public void put(RecipeKeyword mKeyword, List<RecipeItem> recipteItems) {
		if (recipteItems != null && !recipteItems.isEmpty()) {
			List<RecipeItem> cached = getCached(mKeyword);
			cached.addAll(recipteItems);
		}
	}

	@Override
	public void release() {
		if (mData != null) {
			mData.clear();
		}
		mData = null;
	}

}
